package jtk.project4.fleet.task;

import jtk.project4.fleet.domain.TireInv;
import jtk.project4.fleet.event.TireInventoryEvent;
import jtk.project4.fleet.event.TireInventoryEvent.TireInventoryEventType;
import jtk.project4.fleet.service.DatabaseService;

import java.util.List;

import nl.coderight.jazz.Task;
import nl.coderight.jazz.event.MessageEvent;
import nl.coderight.jazz.event.ProgressEvent;
import nl.coderight.jazz.event.MessageEvent.MessageEventType;
import nl.coderight.jazz.event.ProgressEvent.ProgressEventType;

public class LoadTireInvTask extends Task<List<TireInv>>{
	
	@Override
	public List<TireInv> process() throws Exception{
		return DatabaseService.getInstance().getTireinvs();
	}
	
	@Override
	protected void onStart() {
		ProgressEvent progressEvent = new ProgressEvent(ProgressEventType.START);
		progressEvent.setMessage("Loading tireinv...");
		progressEvent.setIndeterminate(true);
		postEvent(progressEvent);
	}
	
	@Override
	protected void onFinish() {
		postEvent(new ProgressEvent(ProgressEventType.STOP));
		TireInventoryEvent tireEvent = new TireInventoryEvent(TireInventoryEventType.LOAD_SUCCESS);
		tireEvent.setTireinvs(getResult());
		postEvent(tireEvent);
	}
	
	@Override
	protected void onError() {
		MessageEvent error = new MessageEvent(MessageEventType.ERROR);
		error.setMessage(getError().getMessage());
		postEvent(error);			
	}

}
